package com.yuxi.config;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClient;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import java.io.IOException;

/**
 * Created by yuxi on 16/11/7.
 */
@Configuration
@PropertySource("classpath:application.properties")
public class MemcacheConfig implements EnvironmentAware {

    private Environment env;
    @Override
    public void setEnvironment(Environment environment) {
        this.env=environment;
    }

    @Bean
    public MemcachedClient getClient() {
        try {
            return new XMemcachedClient( env.getProperty("spring.memcache.host"),
                    Integer.valueOf(env.getProperty("spring.memcache.port")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
